import logging
import time
from typing import List
import os
import yaml
import requests
from ruamel import yaml
from id_card.card import getRandomIdNumber
os.environ["http_proxy"] = ""
os.environ["https_proxy"] = ""


# hrp convert demo.har --to-pytest

def get_pc_token():
    '''
    pc端获取token
    :return: token
    '''
    url = os.environ.get('HOST') + '/yjjk-gateway/yjjk-pt-api/v1/user/login'
    # 测试线登录
    json ={
                    "account": "EnfqeJwKOn4vW31QoIrvSp1vWdgkUJbiFO/rp08U9ZqmKhuiQwHev+RJSoikGyuT5CJ3seLTwxU9Z2TKBrlyI1iJs29XcFR4v1+1KE2NOtZ25TzLdQSr4uyfVsInlKPGI3bdUIK3XVAbNVyQmnhluDzJqsbRHmdpfn7WcFohoPY=",
                    "encryption": 1,
                    "mkId": "",
                    "moduleId": "",
                    "password": "YZYAs4sPcq+leSwtbZvRLykWTA9I5pU/sW9cc4Tq45TMkXChjUigKGSsXGZmn1bXnMN5TI7vCyGiXItiWmayyablZrIJptCRuS6+VqvVP3SSJV0XgQ2hoKJbz5gqVpemnIFMVQy4eZhW2mF85l7q9ByZsu6WwgP7CCqz4iCpwC8=",
                }

    # 线上登录
    # json = {"account":"CniZzLds2P0POv56hEzM8jIPkzc2cOY3Vj+XeLl/Av4/nHfUiBj2aNyVBs6VaaZcswZlQuKfj4a8PyVDjGSHLNOi9rwE4M3sIqp401eE65KLhQcVJHMlLW0Ano2Wf+ZA97rKfEJ2aC0nckrYLOO22l0QVQ2A/o2vglLYMA0ucE8=","password":"UHKrVv5XSHF9ZU+eYjTD3EcwId8xe3sFy0njwkRBRQFtopvF5h3VnGYIanfTT6cBJ8SFYCgCoJl+bEDYRQUQ0Xg1BvQlQZKVdg+WyzxVI1Egma7ZQ0TqTPj0rpQyAcPmHE/WvsaZ72W347RB2CkhvffMHubYYpLLk29eG6YDwP0=","encryption":1,"moduleId":"","mkId":""}
    response = requests.post(url=url, json=json, verify=False).json()
    return response['data']['password']

def get_id_card():
    '''
    :return: 返回一个随机的身份证号
    '''
    return getRandomIdNumber(0)

def get_yaml_by_key(key):
    '''
    返回
    :return: 根据key返回yaml中的value
    '''
    curPath = os.path.dirname(os.path.realpath(__file__))
    # 获取yaml文件路径
    yamlPath = os.path.join(curPath, "env.yaml")
    print(yamlPath)
    with open(yamlPath) as f:
        content = f.read()
        d = yaml.load(content)
    return d[key]

def get_yaml_by_key_add_one(key):
    mobile = get_yaml_by_key(key)
    change_yaml(key, mobile + 1)
    mobile = get_yaml_by_key(key)
    return mobile

def change_yaml(key, value):

    # 获取当前脚本所在文件夹路径
    curPath = os.path.dirname(os.path.realpath(__file__))
    # 获取yaml文件路径
    yamlPath = os.path.join(curPath, "env.yaml")
    with open(yamlPath, encoding="utf-8") as f:
        content = yaml.load(f, Loader=yaml.RoundTripLoader)
        # 修改yml文件中的参数
        content[key] = value
    with open(yamlPath, 'w', encoding="utf-8") as nf:
        yaml.dump(content, nf, Dumper=yaml.RoundTripDumper)

def url():
    return os.environ.get('HOST')

def return_name():
    '''
    返回用户name
    :return:接口自动化+时间戳
    '''
    return '接口自动化'+str(int(time.time()))

def get_user_agent():
    return "hrp/funppy"

def sleep(n_secs):
    time.sleep(n_secs)


def sum(*args):
    result = 0
    for arg in args:
        result += arg
    return result


def sum_ints(*args: List[int]) -> int:
    result = 0
    for arg in args:
        result += arg
    return result


def sum_two_int(a: int, b: int) -> int:
    return a + b


def sum_two_string(a: str, b: str) -> str:
    return a + b


def sum_strings(*args: List[str]) -> str:
    result = ""
    for arg in args:
        result += arg
    return result


def concatenate(*args: List[str]) -> str:
    result = ""
    for arg in args:
        result += str(arg)
    return result


def setup_hook_example(name):
    logging.warning("setup_hook_example")
    return f"setup_hook_example: {name}"


def teardown_hook_example(name):
    logging.warning("teardown_hook_example")
    return f"teardown_hook_example: {name}"
